package com.itrui.order.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itrui.feign.client.ProductClient;
import com.itrui.feign.client.UserClient;
import com.itrui.feign.client.common.Picture;
import com.itrui.feign.client.pojo.Address;
import com.itrui.feign.client.pojo.Product;
import com.itrui.feign.client.pojo.User;
import com.itrui.order.common.Code;
import com.itrui.order.common.R;
import com.itrui.order.common.Result;
import com.itrui.order.pojo.Orders;
import com.itrui.order.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private ProductClient productClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;




    /**
     * 添加订单
     *
     * @param order
     * @return
     */
    @PostMapping
    public Result buyProdcut(@RequestBody Orders order) {
        log.info("订单信息：" + order);

        //发送消息给商品服务，商品是否还在
        rabbitTemplate.convertAndSend("itrui.direct","pro",order.getProductId());

        /*//如果商品没有库存
        int stock = productClient.getStock(order.getProductId());
        if (stock <= 0) {
            return new Result(Code.SAVE_ERR, "商品已售罄");
        }*/
        Map<String,Object> msg = new HashMap<>();
        msg.put("money",order.getAmount());
        msg.put("user",order.getUserId());
        //发送消息给用户服务更改用户余额
        rabbitTemplate.convertAndSend("itrui.direct","user",msg);

        //添加订单
        ordersService.save(order);
        return new Result(Code.SAVE_OK, "下单成功");

    }

    /**
     * 查找用户订单
     *
     * @param account
     * @return
     */
    @GetMapping("/{account}")
    public Result FindAllOrder(@PathVariable Integer account) {
        com.itrui.feign.client.common.Result result = userClient.userInfo(account);
        User one = (User) result.getBody();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, one.getId());
        List<Orders> ordersList = ordersService.list(queryWrapper);

        ordersList = ordersList.stream().map((item) -> {
            Long id = item.getProductId();
            Product productById = productClient.getProductById(id);
            item.setProduct(productById);
            return item;
        }).collect(Collectors.toList());

        return new Result(Code.GET_OK, ordersList, "查询成功");

    }

    ;

    /**
     * 确认收货
     *
     * @param order
     * @return
     */
    @PutMapping
    public R<String> confirmReceipt(@RequestBody Orders order) {

        order.setStatus(4);
        boolean b = ordersService.updateById(order);

        return b ? R.success("已收货") : R.error("失败");
    }

    ;

    /**
     * 订单明细
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result FindAllOrder(@PathVariable Long id) {
        Orders order = ordersService.getById(id);
        com.itrui.feign.client.common.Result result = userClient.userInfo(order.getUserId().byteValue());
        User user = (User) result.getBody();
        order.setUser(user);

        Product productById = productClient.getProductById(order.getProductId());
        order.setProduct(productById);

        com.itrui.feign.client.common.Result result1 = userClient.getAllAddressByAccount(order.getUserId());
        Address address = (Address) result.getBody();
        order.setAddressBook(address);

        return new Result(Code.GET_OK, order, "查询成功");

    }

    ;

}
